package com.smart.meetingDemo.service;


import com.google.gson.JsonParser;
import com.lark.oapi.Client;
import com.lark.oapi.core.utils.Jsons;
import com.lark.oapi.service.im.v1.model.CreateMessageReq;
import com.lark.oapi.service.im.v1.model.CreateMessageReqBody;
import com.lark.oapi.service.im.v1.model.CreateMessageResp;
import lombok.extern.slf4j.Slf4j;
import org.springframework.ai.tool.annotation.Tool;
import org.springframework.stereotype.Service;

import java.nio.charset.StandardCharsets;
import java.util.List;
import java.util.UUID;

@Slf4j
@Service
public class FeishuService {

    @Tool(description = "通过飞书API发送批量消息")
    public void sendMessage(List<String> receiveIds, String content) throws Exception {

        for (String receiveId : receiveIds) {
            Client client = Client.newBuilder("cli_a88e00fb857cd00d", "gRKvzK6bwIyZO5ySpOxL8cTV2UTVMuio").build();
            CreateMessageReq req = CreateMessageReq.newBuilder()
                    .receiveIdType("user_id")
                    .createMessageReqBody(CreateMessageReqBody.newBuilder()
                            .receiveId(receiveId)
                            .msgType("text")
                            .content("{\"text\":\"" + content + "\"}")
                            .uuid(UUID.randomUUID().toString())
                            .build())
                    .build();

            CreateMessageResp resp = client.im().v1().message().create(req);

            if (!resp.success()) {
                System.out.println(String.format("code:%s,msg:%s,reqId:%s, resp:%s",
                        resp.getCode(), resp.getMsg(), resp.getRequestId(), Jsons.createGSON(true, false).toJson(JsonParser.parseString(new String(resp.getRawResponse().getBody(), StandardCharsets.UTF_8)))));
            } else {
                System.out.println(Jsons.DEFAULT.toJson(resp.getData()));
            }
        }
    }
}
